﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simple.Common.Authentication;

namespace Simple.Services.System
{
    public class DynamicLikeService : BusinessBaseService
    {
        private readonly SimpleDbContext _context;

        public DynamicLikeService(SimpleDbContext context,
            ISimpleService services) : base(services)
        {
            _context = context;
        }

        public async Task<int> IsLike(DynamicLikeInputModel model)
        {
            var userId = _currentUser?.FindClaimValue(SimpleClaimTypes.UserId);
            if (model.IsLike)
            {
                LoginType? loginType = null;
                var relationshipValue = string.Empty;
                var userName = _currentUser?.FindClaimValue(SimpleClaimTypes.UserName);
                var loginTypeValue = _currentUser?.FindClaimValue(SimpleClaimTypes.LoginType);
                if (!string.IsNullOrWhiteSpace(loginTypeValue))
                {
                    loginType = (LoginType)Enum.Parse(typeof(LoginType), loginTypeValue);
                }
                var relationship = _currentUser?.FindClaimValue(SimpleClaimTypes.Relationship);
                if (!string.IsNullOrWhiteSpace(relationship))
                {
                    relationshipValue = EnumHelper.GetDescriptionContent((EnumParentRelationship)Enum.Parse(typeof(EnumParentRelationship), relationship));
                }
                var childId = _currentUser?.FindClaimValue(SimpleClaimTypes.ChildId);
                switch (loginType)
                {
                    case LoginType.ParentSide:
                        var child = await _context.Set<Childrens>().FirstOrDefaultAsync(p => p.Id.ToString() == childId);
                        if (child != null)
                        {
                            userName = child.Name + relationshipValue;
                        }
                        break;
                    case LoginType.GardenSide:
                        relationshipValue = "老师";
                        userName += relationshipValue;
                        break;
                    default:
                        break;
                }
                if (!await _context.Set<DynamicLike>().AnyAsync(p => p.ClassDynamicId == model.ClassDynamicId && p.ParentOrTeacherId == new Guid(userId))) {
                    var dynamicLike = new DynamicLike()
                    {
                        ClassDynamicId = model.ClassDynamicId,
                        ParentOrTeacherId = new Guid(userId),
                        LoginType = loginType,
                        LikeDate = DateTime.Now,
                        LikeParentName = userName,
                        Relationship = relationshipValue,
                    };
                    var entityChild = await _context.AddAsync(dynamicLike);
                }
            }
            else
            {
                var dynamicLike = await _context.Set<DynamicLike>().FirstOrDefaultAsync(p => p.ClassDynamicId == model.ClassDynamicId && p.ParentOrTeacherId.ToString() == userId);
                if (dynamicLike == null)
                {
                    throw AppResultException.Status404NotFound("没有找到点赞记录，取消点赞失败");
                }
                _context.Remove(dynamicLike);
            }
            return await _context.SaveChangesAsync();
        }
    }
}
